package com.dbpms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dbpms.mapper.ChamberMapper;
import com.dbpms.mapper.StudentManageMapper;
import com.dbpms.mapper.TransferMapper;
import com.dbpms.pojo.ChamberInfo;
import com.dbpms.pojo.StudentInfo;
import com.dbpms.pojo.TransferChamberInfo;
import com.dbpms.service.ITransferService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TransferServiceImpl  extends ServiceImpl<TransferMapper, TransferChamberInfo> implements ITransferService {

    @Resource
    private TransferMapper transferMapper;
    @Resource
    private StudentManageMapper studentManageMapper;
    @Resource
    private ChamberMapper chamberMapper;

    /**
     * 添加调寝申请
     * @param info
     * @return
     */
    @Override
    public boolean addTransferInfo(TransferChamberInfo info,int student_id) {

        boolean pd = false;

        TransferChamberInfo jc = lambdaQuery()
                .eq(TransferChamberInfo::getApplyChamberStuId,student_id)
                .eq(TransferChamberInfo::getApplyChamberSchedule,"申请中")
                .one();
        if (jc == null){
            // 该学生目前没有报告在申请中，可以再次提交报告
            pd = save(info);
        }
        return pd;
    }

    /**
     * 按照学号id查找调寝进度
     * @param stu_id : 学号id
     * @return
     */
    @Override
    public List<TransferChamberInfo> queryStuApplyTransferInfo(int stu_id) {
        QueryWrapper<TransferChamberInfo> wrapper = new QueryWrapper<TransferChamberInfo>()
                .eq("si.apply_chamber_stu_id",stu_id);
        return transferMapper.queryWhereTransferInfos(wrapper);
    }

    /**
     * 查找正在审核中的调寝进度
     * @return
     */
    @Override
    public List<TransferChamberInfo> queryApplyInScheduleInfo() {
        QueryWrapper<TransferChamberInfo> wrapper = new QueryWrapper<TransferChamberInfo>()
                .eq("si.apply_chamber_schedule","申请中");
        return transferMapper.queryWhereTransferInfos(wrapper);
    }

    /**
     * 将选中的调寝记录审核通过
     * @param apply_other_chamber_id
     * @param old_chamber_id
     * @param new_chamber_id
     * @param stu_id
     * @return
     */
    @Override
    public int successAdjustmentApplyInfo(int apply_other_chamber_id, int old_chamber_id, int new_chamber_id, int stu_id) {
        int pd = 0;
        // 修改成申请成功
        lambdaUpdate()
                .set(TransferChamberInfo::getApplyChamberSchedule,"申请成功") // 审核状态
                .eq(TransferChamberInfo::getApplyOtherChamberId,apply_other_chamber_id)
                .update();
        // 学生信息表的寝室id更新
        UpdateWrapper<StudentInfo> wrapper_1 = new UpdateWrapper<StudentInfo>()
                .set("student_chamber_id",new_chamber_id)
                .in("student_id",stu_id);
        studentManageMapper.update(null,wrapper_1);
        // 寝室表更新（旧寝室人数量-1）
        UpdateWrapper<ChamberInfo> wrapper_2 = new UpdateWrapper<ChamberInfo>()
                .setSql("chamber_number_of_people= chamber_number_of_people - 1")
                .in("chamber_id",old_chamber_id);
        chamberMapper.update(null,wrapper_2);


        // 寝室表更新（新寝室人数量+1）
        UpdateWrapper<ChamberInfo> wrapper_3 = new UpdateWrapper<ChamberInfo>()
                .setSql("chamber_number_of_people = chamber_number_of_people + 1")
                .in("chamber_id",new_chamber_id);
        pd = chamberMapper.update(null,wrapper_3);


        return pd;
    }

    /**
     * 将选中的调寝记录审核打回
     * @param apply_other_chamber_id : 审核id
     * @return
     */
    @Override
    public boolean dangerAdjustmentApplyInfo(int apply_other_chamber_id) {
        return  lambdaUpdate()
                .set(TransferChamberInfo::getApplyChamberSchedule,"申请失败") // 审核状态
                .eq(TransferChamberInfo::getApplyOtherChamberId,apply_other_chamber_id)
                .update(); // 执行;
    }
}
